Imports Microsoft.VisualBasic
Imports System.Collections.Generic

Public NotInheritable Class CookieHelper

    Private Sub New()
    End Sub

    Private Shared TwoWeek As Integer = 14

#Region "Save Cookie"

    ''' <summary>
    ''' Save Cookie and never expires.
    ''' </summary>
    Public Shared Sub Save(ByVal name As String, ByVal value As Object)
        Dim cook As New HttpCookie(name, value)
        cook.Expires = DateTime.MaxValue
        SaveCookie(cook)
    End Sub

    ''' <summary>
    ''' Save Cookie and set expires time.
    ''' </summary>
    Public Shared Sub Save(ByVal name As String, ByVal value As String, ByVal expires As Integer, Optional ByVal expiresType As ExpiresTimeType = ExpiresTimeType.Week)
        Dim cook As New HttpCookie(name, value)
        SetExpriesTime(cook, expires, expiresType)
        SaveCookie(cook)
    End Sub

    Public Shared Sub Save(ByVal name As String, ByVal values As NameValueCollection)
        Dim cook As New HttpCookie(name)
        For Each key As String In values.AllKeys
            cook(key) = values.Item(key)
        Next
        cook.Expires = DateTime.MaxValue
        SaveCookie(cook)
    End Sub

    Public Shared Sub Save(ByVal name As String, ByVal values As NameValueCollection, ByVal expires As Integer, Optional ByVal expiresType As ExpiresTimeType = ExpiresTimeType.Week)
        Dim cook As New HttpCookie(name)
        For Each key As String In values.AllKeys
            cook(key) = values.Item(key)
        Next
        SetExpriesTime(cook, expires, expiresType)
        SaveCookie(cook)
    End Sub

#End Region

#Region "Get Cookie Value"

    Public Shared Function GetValue(ByVal name As String) As String
        If HttpContext.Current.Request.Cookies(name) IsNot Nothing Then
            Return HttpContext.Current.Request.Cookies(name).Value
        End If
        Return Nothing
    End Function

    Public Shared Function GetValues(ByVal name As String) As NameValueCollection
        If HttpContext.Current.Request.Cookies(name) IsNot Nothing Then
            Return HttpContext.Current.Request.Cookies(name).Values
        End If
        Return Nothing
    End Function

    Public Shared Function GetValue(ByVal cookitName As String, ByVal keyName As String)
        If HttpContext.Current.Request.Cookies(cookitName) IsNot Nothing Then
            Dim cookValue As String = HttpContext.Current.Request.Cookies(cookitName).Value
            Dim itemkeyName As String = keyName & "="
            If cookValue.IndexOf(itemkeyName) <> -1 Then
                Return HttpContext.Current.Request.Cookies(cookitName)(keyName)
            End If
        End If
        Return Nothing
    End Function

#End Region

#Region "Edit and Delete Cookie"

    Public Shared Function Edit(ByVal cookieName As String, ByVal keyName As String, ByVal value As String, ByVal expires As Integer, Optional ByVal expiresType As ExpiresTimeType = ExpiresTimeType.Week) As Boolean
        If HttpContext.Current.Response.Cookies(cookieName) IsNot Nothing Then
            Dim cook As HttpCookie = HttpContext.Current.Request.Cookies(cookieName)
            cook(keyName) = value
            SetExpriesTime(cook, expires, expiresType)
            SaveCookie(cook)
            Return True
        End If
        Return False
    End Function

    Public Shared Sub Delete(ByVal name)
        If HttpContext.Current.Request.Cookies(name) IsNot Nothing Then
            Dim cook As New HttpCookie(name)
            cook.Expires = Now.AddDays(-1)
            SaveCookie(cook)
        End If
    End Sub

    Public Shared Sub Delete(ByVal cookName As String, ByVal keyname As String, ByVal expires As Integer, Optional ByVal expiresType As ExpiresTimeType = ExpiresTimeType.Week)
        If HttpContext.Current.Request.Cookies(cookName) IsNot Nothing Then
            Dim cook As HttpCookie = HttpContext.Current.Response.Cookies(cookName)
            cook.Values.Remove(keyname)
            SetExpriesTime(cook, expires, expiresType)
            SaveCookie(cook)
        End If
    End Sub

#End Region

#Region "Other method or enum"

    Private Shared Sub SaveCookie(ByVal cook As HttpCookie)
        HttpContext.Current.Response.Cookies.Add(cook)
    End Sub

    Private Shared Sub SetExpriesTime(ByRef cook As HttpCookie, ByVal expires As Integer, ByVal expiresType As ExpiresTimeType)
        Select Case expiresType
            Case ExpiresTimeType.Second
                cook.Expires = Now.AddSeconds(expires)
            Case ExpiresTimeType.Minute
                cook.Expires = Now.AddMinutes(expires)
            Case ExpiresTimeType.Hour
                cook.Expires = Now.AddHours(expires)
            Case ExpiresTimeType.Day
                cook.Expires = Now.AddDays(expires)
            Case ExpiresTimeType.Month
                cook.Expires = Now.AddMonths(expires)
            Case ExpiresTimeType.Week
                cook.Expires = Now.AddDays(expires * 7)
            Case ExpiresTimeType.Year
                cook.Expires = Now.AddYears(expires)
            Case ExpiresTimeType.Never
                cook.Expires = DateTime.MaxValue
        End Select
    End Sub

    Public Enum ExpiresTimeType
        Second
        Minute
        Hour
        Day
        Week
        Month
        Year
        Never
    End Enum

#End Region

End Class
